const initialState = {
  isFetching: false,
  didInvalidate: false,
  items: []
}

const users = (state = initialState, action) => {
  switch (action.type) {
    case 'INVALIDATE_USERS':
      return {
        ...state,
        didInvalidate: true
      }
    case 'REQUEST_USERS':
      console.log('redicers---REQUEST_USERS');
      return {
        ...state,
        isFetching: true,
        didInvalidate: false
      }
    case 'RECEIVE_USERS_SUCCESS':
      return {
        ...state,
        isFetching: false,
        didInvalidate: false,
        items: action.users,
        lastUpdated: action.receivedAt
      }
    case 'RECEIVE_USERS_FAILURE':
      return {
        ...state,
        isFetching: false,
        didInvalidate: false,
        err: action.error
      }
    
    default:
      return state
  }
}

const usersFetchState = (state = { }, action) => {
  switch (action.type) {
    case 'INVALIDATE_USERS':
    case 'REQUEST_USERS':
    case 'RECEIVE_USERS_SUCCESS':
    case 'RECEIVE_USERS_FAILURE':
      return {
        ...state,
        users: users(state['users'], action)
      }
    default:
      return state
  }
}
module.exports = {usersFetchState}
